﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MusicStore.Silverlight.Helpers;
using MusicStore.Silverlight.ViewModels;
using Castle.Core.Logging;
using MusicStore.Silverlight.Infrastructure;
using Structura.Silverlight.Infrastructure.ViewModel;

namespace MusicStore.Silverlight.Views
{
    public partial class Search : Page, ISearchView
    {
        protected Search()
        {
            InitializeComponent();

            Loaded += Search_Loaded;
        }

        public Search(ISearchViewModel viewModel)
            : this()
        {
            (this as IView).ViewModel = viewModel;
        }

        public ILogger Logger { get; set; }

        IViewModel IView.ViewModel { get { return DataContext as IViewModel; } set { DataContext = value; } }

        public ISearchViewModel ViewModel { get { return DataContext as ISearchViewModel; } }

        void Search_Loaded(object sender, RoutedEventArgs e)
        {
            // use IoC to initialize this member variable
            // ViewModel = App.Container.Resolve<ISearchViewModel>();

            //todo: to remove, due to a bug in castle windsor
            // ViewModel.SearchCommand._viewModel = ViewModel;

            // set the whole data context of the form
            // DataContext = ViewModel;

            // check if we asked for this search directly by navigating to this page
            // this should be maybe moved into the ViewModel 
            if (NavigationContext.QueryString.Count > 0)
            {
                object query = NavigationContext.QueryString["q"];
                if (query != null)
                    ViewModel.PerformSearch(query.ToString());
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        //private void BtnSearch_Click(object sender, RoutedEventArgs e)
        //{
        //    Logger.Info("UI - Performing a Search");

        //    ViewModel.PerformSearch(TxtSearch.Text);
        //}

        //private void ItemClick(object sender, MouseButtonEventArgs e)
        //{
        //    int id = (int)((FrameworkElement)sender).Tag;

        //    Logger.Info(string.Format("UI - Opening Album({0})", id));

        //    NavigateToAlbum(id);
        //}

        //private void NavigateToAlbum(int id)
        //{
        //    Logger.Info(string.Format("UI - Navigating to Album({0})", id));

        //    NavigationService.Navigate(new Uri(string.Format("/Album/{0}", id), UriKind.Relative));
        //}

        //private void BtnAddNewAlbum_Click(object sender, RoutedEventArgs e)
        //{
        //    Logger.Info("UI - Creating a New Album");

        //    NavigateToAlbum(0);
        //}

    }
}
